-
Notifications
You must be signed in to change notification settings - Fork 508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/Improve some typings #909
base: master
Are you sure you want to change the base?
Conversation
- added @types/babel__core, @types/babel__traverse, and @types/lodash.merge and removed their declarations in env.d.ts - also removed rollup-plugin-terser's declaration there because it has types built-in - remaining actually don't have types per in-line comments - this improved type-checking allowed TS to do stricter checks and so exposed a few type errors that were fixed as well - @rollup/plugin-babel depends on @types/babel__core as well, so the declaration here had previously overridden it - `custom` had to be augmented as it's not an option of `@rollup/plugin-babel` - `passPerPreset` did too though... it's not in `@types/babel__core` but it is a (deprecated) Babel option - logs seemed to show it was `false` regardless of what was passed in though... - Rollup's `plugins` now started complaining that `false` wasn't a valid Plugin either, so added a `.filter(Boolean)` but that didn't fully fix it so had to type-cast
- Jared had asked in a review comment back in December to consistently use capital TSDX (not `tsdx`) so this establishes that throughout the rest of the codebase - I don't think any of these lines/files/variables were written by me, so I'm just fixing up existing ones - this came up as I was refactoring typings, so wanted to make sure everything was consistent as I went - rename variable TsdxOptions to TSDXOptions - rename babelPluginTsdx to babelPluginTSDX - and rename the file to this as well - and links to the file in the docs - clarify extractErrors error message to specify `tsdx build`, not just "tsdx" - use TSDX instead of `tsdx` in template docs - don't change contributing docs though, because in that one it's referring to the bin global or repo `tsdx`, which is correct
- previously I had used the `[typeX, ...typeX[]]` TS syntax in types, which means an array of at least one of those types - I actually had to look up how to do this when I originally wrote it and it even confused Shawn when I did - now I've looked at a few times and it takes me a second to realize what that means again, so hopefully this change makes it easier to understand the type at-a-glance and has less mental overhead
- `RollupOptions & { output: OutputOptions }` was used a few times in the codebase, so create a type makes this re-usable and consistent - so it doesn't have to be augmented everywhere in-line like that - and also documents its meaning
- another PR by a contributor exposes the types for `tsdx.config.js` in order to be used in JSDoc, so this makes it so only a single type is needed and exported - the exact type needed is the only exported basically - and that we're not re-exporting Rollup types directly
This comment was marked as spam.
This comment was marked as spam.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks good to me. Mostly just straightforward renames.
The external types portion is the most potentially confusing bit, since the type-checking just ignored some Babel and Rollup stuff before due to the lack of declarations. Babel stuff is necessary and fairly straightforward at least.
A few things I'd like to take a closer look at though
@@ -213,6 +213,6 @@ export async function createRollupConfig( | |||
toplevel: opts.format === 'cjs', | |||
warnings: true, | |||
}), | |||
], | |||
].filter(Boolean) as Plugin[], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a simple one and doesn't change any behavior, but the type-cast seemed to have eroded some typing requirements that exist without it. E.g. Rollup requires each Plugin
to have a name
property, and some of these custom ones here do not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh think I need a type predicate here per microsoft/TypeScript#20812. I've been using these type-casts after filter
like #54 did, but a type predicate / type guard is the right way to do it that I believe will prevent eroding of types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I tried the below:
].filter((e): e is Plugin => Boolean(e))
but that didn't fully work either. It also eroded some types, like the name
property requirement mentioned above 😕
With the TSDXOptions
cast it didn't work because it doesn't seem to be able to infer the AtLeastOne
portion 😕 Using enums didn't help with that either 😕
But I was able to fix some type erosion since the esm
format one doesn't fit the type since it's missing env
. Need to link those two together somehow...
For this Rollup config piece, I was able to rewrite the condition && plugin
to ...(condition ? [plugin] : [])
which I've done before elsewhere, and then that worked, but it is quite verbose... maybe I can abstract that into a function...
export interface TSDXConfig { | ||
rollup: (config: RollupOptions, options: TSDXOptions) => RollupOptions; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still has the same problem as in #823 (comment) . Should probably try being more specific here and in createRollupConfig.ts
.
Description
Commits
types: add remaining external typings
added @types/babel__core, @types/babel__traverse, and
@types/lodash.merge and removed their declarations in env.d.ts
has types built-in
this improved type-checking allowed TS to do stricter checks and so
exposed a few type errors that were fixed as well
declaration here had previously overridden it
custom
had to be augmented as it's not an option of@rollup/plugin-babel
passPerPreset
did too though... it's not in@types/babel__core
but it is a (deprecated) Babel option
false
regardless of what waspassed in though...
plugins
now started complaining thatfalse
wasn't avalid Plugin either, so added a
.filter(Boolean)
but that didn'tfully fix it so had to type-cast
refactor/fix: consistently use capital TSDX everywhere
Jared had asked in a review comment back in December to consistently
use capital TSDX (not
tsdx
) so this establishes that throughout therest of the codebase
so I'm just fixing up existing ones
this came up as I was refactoring typings, so wanted to make sure
everything was consistent as I went
refactor/types: explicitly name "at least one" types
refactor/types: add a type for RollupOptionsWithOutput
refactor/types: add a TSDXConfig type
tsdx.config.js
inorder to be used in JSDoc, so this makes it so only a single type is
needed and exported
Tags
tsdx
, "tsdx", etc was requested in (docs): add warning to tsdx.config.js usage #400 (comment). Maybe in an earlier issue/PR too, but I couldn't find or remember it.TSDXConfig
type is pretty much just to simplify feat/types: add main and types fields #823Review Notes
The
passPerPreset
augmentation for@rollup/plugin-babel
/@types/babel__core
that is mentioned above is pretty confusing. I decided to leave it as is for now so as to potentially change anything, but my logs seem to show it isn't used 😕 🤔 :Babel log output:
Misc
This will be rebased in as each commit is independent and some have some lengthy messages.